package com.braydeejohnson.untitled;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.nio.ShortBuffer;
import java.util.ArrayList;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;
import android.util.Log;

public class GlRenderer implements Renderer {

	private Square square;
	private Context context;
	private float xt;
	private float yt;
	private Main main;
	private MainThread thread;
	private static final String TAG = GlRenderer.class.getSimpleName();
	private static final int MAX_FRAMES = 60;
	private static final int MAX_SKIPS = 5;
	private static final int FRAME_PERIOD = 1000/MAX_FRAMES;
	private int fps = 0;
	private int ticks = 0;
	private int lastRender = 0;
	private long time;
	private long beginTime;
	private long endTime;
	private long timeDiff;
	private int sleepTime = 0;
	int framesSkipped = 0;
	private float vertices[];
	private float[] colors;
	private short[] indicies;
	private FloatBuffer vertexBuffer;
	private FloatBuffer textureBuffer;
	private FloatBuffer interleavedBuffer;
	private FloatBuffer colorBuffer;
	private ShortBuffer indexBuffer;

	
	public GlRenderer(Context context,Main main){
		//this.triangle = new Triangle();
		this.context = context;
		this.main = main;
		this.xt=0;
		this.yt=0;
	}
	
	private void render(GL10 gl){
		
		gl.glPushMatrix();
		gl.glFrontFace(GL10.GL_CW);
		gl.glEnable(GL10.GL_CULL_FACE);
		gl.glCullFace(GL10.GL_BACK);
		gl.glEnableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexBuffer);
		gl.glEnableClientState(GL10.GL_COLOR_ARRAY);
		gl.glColorPointer(4, GL10.GL_FLOAT, 0, colorBuffer);
		gl.glDrawElements(GL10.GL_TRIANGLES, indicies.length, GL10.GL_UNSIGNED_SHORT, indexBuffer);
		gl.glDisableClientState(GL10.GL_COLOR_ARRAY);
		gl.glDisableClientState(GL10.GL_VERTEX_ARRAY);
		gl.glDisable(GL10.GL_CULL_FACE);
		gl.glPopMatrix();
	}
	
	public void onDrawFrame(GL10 gl) {
		
		//Log.d(TAG,"Begin TIME:"+beginTime);
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		gl.glLoadIdentity();
		for(int i=0;i<Main.squares.size();i++){
			
			Main.squares.get(i).draw2(gl);
			
		}
		endTime = System.currentTimeMillis();
		timeDiff = endTime - beginTime;
		//Log.d(TAG,"End Time:"+endTime);
		time+=timeDiff;
		ticks++;
		//Log.d(TAG,"Total Time:"+time);
		if(time>=FRAME_PERIOD*MAX_FRAMES){
		Log.d(TAG,"Frames Per Tick:"+ticks);
		time=0;
		ticks=0;
		
		}
		//Log.d(TAG,"LAG?:"+System.currentTimeMillis());
		beginTime = System.currentTimeMillis();
	}

	public void onSurfaceChanged(GL10 gl, int width, int height) {
		if(height ==0) {
			height=1;
		}
		float ratio = (float) width / height;
		gl.glViewport(0, 0, width, height);
		gl.glMatrixMode(GL10.GL_PROJECTION);
		gl.glLoadIdentity();
		Log.d(TAG, "Width:"+width+" Height:"+height);
		//GLU.gluPerspective(gl, 45.0f,(float)width/(float)height,0.1f, 100.0f);
		GLU.gluOrtho2D(gl, 0, width, 0, height);
		gl.glMatrixMode(GL10.GL_MODELVIEW);
		gl.glLoadIdentity();
		
		
	}

	public void onSurfaceCreated(GL10 gl, EGLConfig config) {
		
		for(int y=0;y<480/64;y++){
			for(int x=0;x<800/64;x++){
				Main.squares.add(this.square = new Square(x,y));
				//square.loadGLTexture(gl, this.context);
			}
		}
		gl.glEnable(GL10.GL_TEXTURE_2D);
		gl.glShadeModel(GL10.GL_SMOOTH);
		gl.glClearColor(0.0f, 0.0f, 0.0f, 0.5f);
		gl.glClearDepthf(1.0f);
		gl.glEnable(GL10.GL_DEPTH_TEST);
		gl.glDepthFunc(GL10.GL_LEQUAL);
		
		gl.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);
		
		thread = new MainThread(this.main,gl);
		thread.start();
		thread.setRunning(true);
		Log.d(TAG, "Thread Created");

	}

}
